<?php

namespace app\api\controller\v48;

use app\api\controller\v13\Auth2;
use app\common\cachemodel\acquired_astro\AppAcquiredAstroAnswerRecord;
use app\common\cachemodel\acquired_astro\AppAcquiredAstroTestRecord;
use think\response\Json;

/**
 * 后天星座 类
 */
class AcquiredAstro extends Auth2
{
    /**
     * 后天星座 首页
     * @return Json
     * @throws \Exception
     */
    public function index()
    {
        $paySuc = input('pay_suc/d', 0);

        $acquiredAstroAnswerRecordModel = new AppAcquiredAstroAnswerRecord();
        $acquiredAstroTestRecordModel = new AppAcquiredAstroTestRecord();
        $pageData = $this->user ? $acquiredAstroTestRecordModel->getListPage($this->user['id']) : ['data' => []];
        $popData = null;
        $btnList = [];
        if (empty($pageData['data']))
        {
            $testAnswerData = $this->user ? $acquiredAstroAnswerRecordModel->getUserAnswerData($this->user['id']) : null; // 获取最近一次答题记录
            if ($testAnswerData['index'] < $testAnswerData['num'])
            {   // 有未答完的题目
                $bList = [
                    [
                        'btn_name' => sprintf("当前进度：%d/%d 继续测试", $testAnswerData['index'], $testAnswerData['num']),
                        'target' => 'local',
                        'link' => 'acquired_astro_examination?type=1&status=1&need_auth=2',
                        'style' => 1,
                    ], [
                        'btn_name' => '重新测试',
                        'target' => 'local',
                        'link' => 'acquired_astro_examination?type=1&status=2&need_auth=2',
                        'style' => 2,
                    ],
                ];
            } else
            {   // 从未答题过
                $bList = [
                    [
                        'btn_name' => '开始测试',
                        'target' => 'local',
                        'link' => 'acquired_astro_examination?type=1&status=1&need_auth=2',
                        'style' => 1,
                    ],
                ];
            }
            $btnList = [
                [
                    'btn_name' => 'VIP体验版(20题)',
                    'must_pay' => 0,
                    'gid' => 0,
                    'target' => '',
                    'link' => '',
                    'pop_data' => [
                        'tip_data' => ['1、建议一次性完成。', '2、答案没有对与错;跟随内心选择做出选择，而不是选择你期待的理想情况。', '3、如果题目中没有完全符合的选项时，请选择和自己想法更相近的一个选项。',],
                        'btn_list' => $bList,
                    ],
                ], [
                    'btn_name' => '专业版(100题)',
                    'must_pay' => 1,
                    'gid' => 159,
                    'target' => 'local',
                    'link' => "pay_order?type=32&gid=159&need_auth=2",
                    'pop_data' => null,
                ],
            ];
        } else
        {   // 判断 测试 记录 里面是否下单购买了
            $isPay = false; //  是否已经购买过了
            $testId = 0;
            $testStatus = 0;
            foreach ($pageData['data'] as $k => $v)
            {
                $v = $acquiredAstroTestRecordModel->dataInit($v);
                if ($v['type'] === 2)
                {   // 完整版
                    $isPay = true;
                    break;
                } else
                {
                    $testId = $v['id'];
                    // 0=>待测试 1=>测试完成等待计算 2=>计算中 3=>计算完成 4=>计算失败
                    if ($v['status'] === 3)
                    {
                        $testStatus = 2;
                    } else
                    {
                        $testStatus = 1;
                    }
                }
            }
            if ($isPay)
            {   //  已经购买 完整版测试了
                $TestData = $acquiredAstroTestRecordModel->getDelayTestData($this->user['id']);
                if ($TestData)
                {
                    $testAnswerData = $acquiredAstroAnswerRecordModel->getUserAnswerData($this->user['id']); // 获取最近一次答题记录
                    if ($testAnswerData['index'] < $testAnswerData['num'] || $testAnswerData['num'] === 20)
                    {   // 有未答完的题目
                        $bList = [
                            [
                                'btn_name' => sprintf("当前进度：%d/%d 继续测试", $testAnswerData['index'], 100),
                                'target' => 'local',
                                'link' => 'acquired_astro_examination?type=2&status=1&need_auth=2',
                                'style' => 1,
                            ], [
                                'btn_name' => '重新测试',
                                'target' => 'local',
                                'link' => 'acquired_astro_examination?type=2&status=2&need_auth=2',
                                'style' => 2,
                            ],
                        ];
                    } else
                    {
                        $bList = [
                            [
                                'btn_name' => '开始测试',
                                'target' => 'local',
                                'link' => 'acquired_astro_examination?type=2&status=1&need_auth=2',  // 开始测试
                                'style' => 1,
                            ],
                        ];

                        if ($paySuc)
                        {
                            $popData = [
                                'tip_data' => ['1、建议一次性完成。', '2、答案没有对与错;跟随内心选择做出选择，而不是选择你期待的理想情况。', '3、如果题目中没有完全符合的选项时，请选择和自己想法更相近的一个选项。',],
                                'btn_list' => $bList,
                            ];
                        }
                    }
                } else
                {
                    $bList = [
                        [
                            'btn_name' => '开始新的测试',
                            'target' => 'local',
                            'link' => 'pay_order?type=32&gid=159&need_auth=2',  // 去购买
                            'style' => 1,
                        ], [
                            'btn_name' => '查看测试历史',
                            'target' => 'local',
                            'link' => 'acquired_astro_my_list?need_auth=2', // 查看历史
                            'style' => 2,
                        ],
                    ];
                }
                $btnList = [
                    [
                        'btn_name' => '专业版(100题)',
                        'must_pay' => 0,
                        'gid' => 159,
                        'target' => '',
                        'link' => "",
                        'pop_data' => [
                            'tip_data' => ['1、建议一次性完成。', '2、答案没有对与错;跟随内心选择做出选择，而不是选择你期待的理想情况。', '3、如果题目中没有完全符合的选项时，请选择和自己想法更相近的一个选项。',],
                            'btn_list' => $bList,
                        ],
                    ],
                ];
            } else
            {   //  没有购买 完整版
                $btnList = [
                    [
                        'btn_name' => '初步测试结果',
                        'must_pay' => 0,
                        'gid' => 0,
                        'target' => 'local',
                        'link' => "acquired_astro_examination_result?id={$testId}&status=$testStatus&need_auth=2",
                        'pop_data' => null,
                    ], [
                        'btn_name' => '专业版(100题)',
                        'must_pay' => 1,
                        'gid' => 159,
                        'target' => 'local',
                        'link' => "pay_order?type=32&gid=159&need_auth=2",
                        'pop_data' => null,
                    ],
                ];
            }
        }

        $resData = [
            'content_img' => [
                '/test/acquired_astro/content1.png',
                '/test/acquired_astro/content2.png',
                '/test/acquired_astro/content3.png',
            ],
            'btn_list' => $btnList,
            'pop_data' => $popData,
        ];

        return show(config('code.success'), '请求成功', $resData);
    }
}